#pragma once
#include <iostream>
#include <memory>
#include <dirent.h>
#include <sys/stat.h>
#include <algorithm>
#include <chrono>
#include<vector>
#include<opencv2/opencv.hpp>
#include <string>
#include "retinaface_det_api.h"
#include "face_landmark_align_api.h"
#include "arcface_feature_gpu_api.h"

using namespace retinaface;
using namespace facealign;
using namespace arcfacefeaturegpu;

int64_t CurrentTimeMillis()
{
        int64_t timems = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
        return timems;
}
void FindFiles(const std::string &path, std::vector<std::string> &files)
{
        struct stat rst;
        if (stat(const_cast<char *>(path.c_str()), &rst) != 0)
            return;
        DIR *dirpt = opendir(path.data());
        dirent *entry;
        while (entry = readdir(dirpt))
        {
                /* code */
                if (entry->d_type == DT_REG)
                        files.emplace_back(entry->d_name);
        }
        closedir(dirpt);
}

void PrintVector(const std::vector<float> vec)
{

    for (auto iter = vec.cbegin(); iter != vec.cend(); iter++)
    {
        std::cout << (*iter) <<" ";
    }
    std::cout <<std::endl;

}



void test_face_pipline()
{
    
    std::shared_ptr<RetinaFaceApi> retinaface_gpu = std::make_shared<RetinaFaceApi>(0,
    "/home/user/yyz_workspace_disk/Code/algolib/input_models/onnx_model/retinaface_mobilenet/FaceDetector_dynamic.onnx",
    "/home/user/yyz_workspace_disk/Code/algolib/input_models/trt_model/",
    8); 
    
    std::shared_ptr<FaceAlignApi> face_align = std::make_shared<FaceAlignApi>(); 
    
    
    
    std::shared_ptr<ArcfaceFeatureGpuApi> feature_gpu = std::make_shared<ArcfaceFeatureGpuApi>(0,
    "/home/user/yyz_workspace_disk/Code/algolib/input_models/onnx_model/insightface_arcface_res18/backbone_dynamic_axes.onnx",
    "/home/user/yyz_workspace_disk/Code/algolib/input_models/trt_model/",
   8);

    std::string file_path = "/home/user/yyz_workspace_disk/Code/face/FaceX-Zoo-main/face_sdk/api_usage/test_images/test1.jpg";
    // std::string file_path = "/bata/Code/face/Pytorch_Retinaface-master/curve/test_resize.jpg";
   
    cv::Mat img = cv::imread(file_path);

    cv::imshow("img", img);

    std::vector<cv::Rect> rectangles;
    std::vector<float> confidence;
    std::vector<std::vector<cv::Point>> landmarks;
    
    
    retinaface_gpu->Execute(img, rectangles, confidence, landmarks);

    std::vector<cv::Mat> faces;
    for (auto i = 0; i < rectangles.size(); i++)
    {
        cv::Mat face;
        cv::Mat face_cut;
        face_cut = img(rectangles[i]);
        face_align->Execute(img ,landmarks[i], face);
        cv::imshow("align"+ std::to_string(i), face);
        cv::imshow("cut"+ std::to_string(i), face_cut);
        faces.push_back(face);
    }
  
    std::vector<std::vector<float>> features = feature_gpu->Execute(faces);
    for(auto i = 0; i <  features.size(); i++)
    {

        PrintVector(features[i]);
        std::cout<<"******************************"<<std::endl;

    }


    cv::waitKey();











}